<?php

namespace App\Http\Controllers;

use App\Models\Activity;
use App\Models\ActivityQuestion;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ActivityQuestionController extends CommonController
{
    /**
     * 获取活动问题列表
     */
    public function getIndex()
    {
        $lists = ActivityQuestion::with('activity', 'user')->orderBy('activity_id', 'asc')->paginate(10);

        return view('activityquestion.index')->with("lists", $lists)->with("actions", $this->actions);
    }

    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
//        dd($data);
        $lists = ActivityQuestion::with('activity', 'user')->orderBy('activity_id', 'asc');

        if (isset($data['activity_name']) && $data['activity_name'] != '') {
            $aIds = Activity::where('subject', 'like', '%' . trim($data['activity_name']) . '%')->lists('id');
            $lists->whereIn('activity_id', $aIds);
        }

        if (isset($data['question']) && $data['question'] != '') {
            $lists->where('question', 'like', '%' . trim($data['question']) . '%');
        }

        if (isset($data['answer']) && $data['answer'] != '') {
            $lists->where('answer', 'like', '%' . trim($data['answer']) . '%');
        }

        $lists = $lists->paginate(10);

        return view('activityquestion.search')->with("lists", $lists)->with("actions", $this->actions)
            ->with('answer', $data['answer'])->with('question', $data['question'])->with('activity_name', $data['activity_name']);
    }

    /**
     * 获取编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = ActivityQuestion::where('id', $data['id'])->first();

        return view('activityquestion.edit')->with('list', $list);
    }

    /**
     * 执行一个编辑
     */
    public function postEdit(Request $request)
    {
        $data = $request->input();
        ActivityQuestion::where('id', $data['id'])->update(['question' => $data['question'], 'answer' => $data['answer'], 'top' => $data['top']]);

        return AjaxCallbackMessage('编辑成功', true, url('activityquestion/index'));
    }

    /**
     * 执行一个删除
     */
    public function postDelete(Request $request)
    {
        $data = $request->input();
        $result = ActivityQuestion::where('id', $data['qid'])->delete();

        if ($result) {
            return AjaxCallbackMessage('删除成功', true, url('activityquestion/index'));
        } else {
            return AjaxCallbackMessage('删除失败，请联系管理员', false);
        }
    }

    /**
     * 获取创建页面
     */
    public function getCreate()
    {

        return view('activityquestion.create');
    }

    /**
     * 执行一个创建
     */
    public function postCreate()
    {
    }
}
